<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2022/3/16
 */

namespace App\Services;

use App\Models\Navigation;

/**
 * 导航服务
 *
 * Class NavigationService
 * @package App\Services
 */
class NavigationService
{

    /**
     * 获取所有导航
     * @param $parent_id // 父导航ID
     * @return mixed
     */
    public static function getAllList($parent_id = null)
    {
        $where = [['status', 1]];
        if ($parent_id != null) {
            $where[] = ['parent_id', $parent_id];
        }
        $resource = Navigation::where($where)->orderBy('sort', 'asc')->orderBy('id', 'desc')->get();
        collect($resource)->each(function ($item) {
            // link
            $item->href = self::getHref($item);
            $item['href_'] = $item->href ? " href =$item->href " : '';
            $item['href_target_'] = $item->href_target ? " target =$item->href_target " : '';
            $item['rel_'] = $item->rel ? " rel =$item->rel " : '';
        });
        return $resource;
    }

    /**
     * 获取导航条
     * @return array
     */
    public static function getBar()
    {
        $getAllList = self::getAllList();
        return TreeService::make($getAllList);
    }

    /**
     * 根据顶级导航标识获取导航条
     * @param string $id_name
     * @return array
     */
    public static function getBarByIdName($id_name = '')
    {
        $result = [];
        if (!$id_name) return $result;
        $getDetailByIdName = self::getDetailByIdName($id_name);
        if ($getDetailByIdName) {
            $getAllList = self::getAllList()->toArray();
            $result = TreeService::getSubsTreeByParentId($getAllList, $getDetailByIdName['id']);
        }
        return $result;
    }

    /**
     * 根据导航ID获取指定导航详情
     * @param int $nav_id
     * @return array|mixed
     */
    public static function getDetail($nav_id = 0)
    {
        $getAllList = self::getAllList();
        if (!$nav_id || !$getAllList) return [];
        $result = collect($getAllList)->first(function ($value, $key) use ($nav_id) {
            return $value['id'] == $nav_id;
        });
        return $result ?: [];
    }

    /**
     * 根据导航标识获取指定导航详情
     * @param string $id_name // 导航标识
     * @return array|mixed
     */
    public static function getDetailByIdName($id_name = '')
    {
        $getAllList = self::getAllList();
        if (!$id_name || !$getAllList) return [];
        $result = collect($getAllList)->first(function ($value, $key) use ($id_name) {
            return $value['id_name'] == $id_name;
        });
        return $result ?: [];
    }

    /**
     * 根据导航项获取导航链接
     * @param $item
     * @return string
     */
    static function getHref($item)
    {
        $href = "";
        if ($item) {
            if ($item->info_category_id > 0) {
                $getCate = InfoCategoryService::getDetail($item->info_category_id);
                $href = $getCate['href']??'';
            } else {
                $href = $item->href ?: $href;
            }
        }
        return $href;
    }

}
